package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hjq.base.DensityUtils;
import com.hjq.toast.ToastUtils;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.adapter.SelectIconAdapter;
import com.qiantu.phone.ui.uiutil.DeviceMainType;
import com.qiantu.phone.ui.uiutil.DeviceType;

import java.util.ArrayList;
import java.util.Arrays;

public class DeviceIconSelectActivity extends AppActivity {

    private RecyclerView imageList;
    private SelectIconAdapter mSelectIconAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_device_icon_select;
    }

    @Override
    protected void initView() {
        imageList = findViewById(R.id.image_list);
        imageList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        int imageListWidth = DensityUtils.getScreenWidth(getContext()) - imageList.getPaddingLeft() - imageList.getPaddingRight();
        imageList.setLayoutManager(new GridLayoutManager(getContext(), 3));
        mSelectIconAdapter = new SelectIconAdapter(imageListWidth / 3, SelectIconAdapter.TYPE_DEVICE_ICON){
            @Override
            public void onSelectChanged(String mSelectedIcon) {
            }
        };
        imageList.setAdapter(mSelectIconAdapter);
        String selectIcon = getIntent().getStringExtra("selectIcon");
        if(!TextUtils.isEmpty(selectIcon)){
            mSelectIconAdapter.setSelectIcon(selectIcon);
        }
    }

    @Override
    public void onRightClick(View view) {
        Intent intent = new Intent();
        intent.putExtra("DATA", mSelectIconAdapter.getSelectIcon());
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void initData() {
        String type = getIntent().getStringExtra("type");
        String deviceType =  getIntent().getStringExtra("deviceType");
        if(TextUtils.isEmpty(type) || TextUtils.isEmpty(deviceType)){
            return;
        }
        String[] iconUrls = null;
        //有些根据type获取图标，有些根据子类型获取
        if(type.equals(DeviceMainType.AIRCONDITIONER.stringType)){
            iconUrls = new String[]{"icon_device_airconditioner","icon_device_verticalair"};
        }else if(type.equals(DeviceMainType.WALL_SWITCH.stringType)){
            iconUrls = new String[]{"icon_device_panelswitch"};
        }else if(type.equals(DeviceMainType.WIRELESS_433.stringType)){
            iconUrls = new String[]{"icon_device_infrared_airconditioner","icon_device_infrared_curtain","icon_device_infrared_freshair","icon_device_infrared_other","icon_device_infrared_remotecontrol", "icon_device_infrared_television"};
        }else if(type.equals(DeviceMainType.WIRELESS_315.stringType)){
            iconUrls = new String[]{"icon_device_infrared_airconditioner","icon_device_infrared_curtain","icon_device_infrared_freshair","icon_device_infrared_other","icon_device_infrared_remotecontrol", "icon_device_infrared_television"};
        }else if(type.equals(DeviceMainType.INFRARED.stringType)){
            iconUrls = new String[]{"icon_device_infrared_airconditioner","icon_device_infrared_curtain","icon_device_infrared_freshair","icon_device_infrared_other","icon_device_infrared_remotecontrol", "icon_device_infrared_television"};
        }else{
            if(deviceType.equals(DeviceType.BREAKER_LIGHT.stringType)){
                iconUrls = new String[]{"icon_device_light","icon_device_light_chandelier","icon_device_light_desklamp","icon_device_light_spotlight","icon_device_light_walllamp","icon_device_light_downlight","icon_device_light_lightstrip"};
            }else if(deviceType.equals(DeviceType.ADJUST_LIGHT.stringType)){
                iconUrls = new String[]{"icon_device_light","icon_device_light_chandelier","icon_device_light_desklamp","icon_device_light_spotlight","icon_device_light_walllamp","icon_device_light_downlight","icon_device_light_lightstrip"};
            }else if(deviceType.equals(DeviceType.ADJUST_COLOR.stringType)){
                iconUrls = new String[]{"icon_device_light","icon_device_light_chandelier","icon_device_light_desklamp","icon_device_light_spotlight","icon_device_light_walllamp","icon_device_light_downlight","icon_device_light_lightstrip"};
            }else if(deviceType.equals(DeviceType.TCT_DL.stringType) || deviceType.equals(DeviceType.ADJUST_LCT.stringType)){
                iconUrls = new String[]{"icon_device_light","icon_device_light_chandelier","icon_device_light_desklamp","icon_device_light_spotlight","icon_device_light_walllamp","icon_device_light_downlight","icon_device_light_lightstrip"};
            }else if(deviceType.equals(DeviceType.ROLLER_SHUTTER.stringType)){
                iconUrls = new String[]{"icon_device_curtain","icon_device_curtain_left","icon_device_curtain_right","icon_device_jalousie","icon_device_rollerblind"};
            }else if(deviceType.equals(DeviceType.WINDOW_CURTAINS.stringType) || deviceType.equals("oc_windowcurtains")){
                iconUrls = new String[]{"icon_device_curtain","icon_device_curtain_left","icon_device_curtain_right","icon_device_jalousie","icon_device_rollerblind"};
            }else if(deviceType.equals(DeviceType.HAILIN_FLOOR_HEATING.stringType)){
                iconUrls = new String[]{"icon_device_floorheating"};
            }else if(deviceType.equals(DeviceType.YILIN_FLOOR_HEATING.stringType)){
                iconUrls = new String[]{"icon_device_floorheating"};
            }else if(deviceType.equals(DeviceType.LANSHE_FRESHAIR.stringType)){
                iconUrls = new String[]{"icon_device_freshair"};
            }else if(deviceType.equals(DeviceType.HUODESENG_FRESHAIR.stringType)){
                iconUrls = new String[]{"icon_device_freshair"};
            }else if(deviceType.equals(DeviceType.HUMAN_BODY_SENSOR.stringType)
             || deviceType.equals(DeviceType.HUMAN_BODY_SENSOR2.stringType)){
                iconUrls = new String[]{"icon_intelligent_environment_infrared"};
            }else if(deviceType.equals("temperature_sensor")){
                iconUrls = new String[]{"icon_room_temperature"};
            }else if(deviceType.equals("humidity_sensor")){
                iconUrls = new String[]{"icon_room_humidity"};
            }else if(deviceType.equals("illuminance_sensor")){
                iconUrls = new String[]{"icon_room_illumination"};
            }else if(deviceType.equals("pm2_5_sensor")){
                iconUrls = new String[]{"icon_room_pm25"};
            }else if(deviceType.equals("noise_sensor")){
                iconUrls = new String[]{"icon_room_noise"};
            }else if(deviceType.equals("door_contact")){
                iconUrls = new String[]{"icon_intelligent_environment_gatemagnetism"};
            }else if(deviceType.equals("human_body_sensor_radar")){
                iconUrls = new String[]{"icon_device_humanbodyinduction"};
            }else if(deviceType.equals("alarm_switch")){
                iconUrls = new String[]{"icon_intelligent_environment_alarm"};
            }else if(deviceType.equals("trigger_switch")){
                iconUrls = new String[]{"icon_intelligent_environment_trigger"};
            }else if(deviceType.equals("smoke_alarm")){
                iconUrls = new String[]{"icon_intelligent_environment_smog"};
            }else if(deviceType.equals("infrared_curtain")){
                iconUrls = new String[]{"icon_intelligent_environment_curtain"};
            }else if(deviceType.equals("vibration_sensing")){
                iconUrls = new String[]{"icon_intelligent_environment_shake"};
            }else if(deviceType.equals("toxic_gas")) {
                iconUrls = new String[]{"icon_intelligent_environment_gas"};
            }else if(deviceType.equals("infrared_contrast")) {
                iconUrls = new String[]{"icon_intelligent_environment_correlation"};
            }else {
                ToastUtils.show("当前设备暂无图标");
            }
        }
        if (iconUrls!=null){
            ArrayList<String> iconArray = new ArrayList<>(Arrays.asList(iconUrls));
            mSelectIconAdapter.setNewInstance(iconArray);
        }
    }
}